package com.gurumades.jsonform.shared.dispatch.validators;

import java.util.Map;

import com.gurumades.jsonform.shared.dispatch.FieldHelper;
import com.gurumades.jsonform.shared.dispatch.JsonHelper;


@SuppressWarnings("rawtypes")
public class RequiredValidator implements FormItemValidator  {
	
	@Override
	public void validate(Object item, Map<String, Object> values,
			ValidatorHelper helper) {
		
		JsonHelper jshelper  = ValidatorHelper.getJsonHelper();
		Map mapItem = jshelper.wrapItem(item);
		
		String fieldID = jshelper.getString(mapItem, "id", null);
		Object val = values.get(fieldID);
		if (jshelper.getBoolean(mapItem, "required", false)) {
			if (FieldHelper.isEmpty(mapItem, val)) {
				helper.addError(fieldID, "You should provide a value for this field.");
			}
		}
	}
	
	@Override
	public boolean supportType(String type) {
		return true;
	}
}
